/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.siga.modelo.DAO;

import com.siga.modelo.entidad.Plan;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author karolyn
 */
public class PlanDAO {
    private EntityManager em;
    private EntityManagerFactory emf;
    
    public PlanDAO(){
        emf=Persistence.createEntityManagerFactory("AppSigaPPU");
        em=emf.createEntityManager();
    }
    
    public void closeEntity(){
        em.close();
    }
    
    public boolean insertarPlan(Plan p){
        boolean flag=false;
        try{
            em.getTransaction().begin();
            em.persist(p);
            em.getTransaction().commit();
            flag = true;
        } catch(Exception e){
            em.getTransaction().rollback();
        }
        return flag;
    }
    
    public Plan buscarPlan(String codigo){
        return em.find(Plan.class, codigo);
    }
    
    public List<Plan> listarPlanes(){
        return em.createQuery("SELECT p FROM Plan p").getResultList();
    }
    
    public boolean eliminarPlan(Plan p){
        boolean flag=false;
        try{
            em.getTransaction().begin();
            em.remove(p);
            em.getTransaction().commit();
            flag = true;
        } catch(Exception e){
            em.getTransaction().rollback();
        }
        return flag;
    }
}
